package dao.daoImpl;

import common.DataSourceUtils;
import dao.LogMapper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.Department;
import pojo.Log;
import pojo.LogFamily;
import pojo.User;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017-11-26.
 */
public class LogMapperImpl implements LogMapper {

    @Override
    public List<LogFamily> getLogFamily() throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from logFamily";
        List<LogFamily> logFamilies= qr.query(sql,new BeanListHandler<LogFamily>(LogFamily.class));
        return logFamilies;

    }

    @Override
    public int writeLog(Log log) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="insert into log (userId,username,title,context,date,logFamily,department,realName) values (?,?,?,?,?,?,?,?)";
        int result=qr.update(sql,log.getUserId(),log.getUsername(),log.getTitle(),log.getContext(),log.getDate(),log.getLogFamily(),log.getDepartment(),log.getRealName());
        return result;
    }

    @Override
    public List<Log> getAllLog(Integer id) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where userId = ?";
        List<Log> logs= qr.query(sql,new BeanListHandler<Log>(Log.class),id);
        return logs;
    }

    @Override
    public int getCount(Integer id) throws SQLException {

        return  0;
    }

    @Override
    public List<Log> getCurrentLog(Integer id, int pageNow, int i) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where userId = ? ORDER BY id desc limit ?,? ";
        List<Log> logs1= qr.query(sql,new BeanListHandler<Log>(Log.class),id,i*(pageNow-1),i);
        return logs1;
    }

    @Override
    public Log lookCurrentLog(int id) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where id = ?";
        Log log= qr.query(sql,new BeanHandler<Log>(Log.class),id);
        return log;
    }

    @Override
    public int againSubmit(Log log) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="update log set title=?,context=?,date=?,logFamily=?,logStatus=? where id=?";
        int result=qr.update(sql,log.getTitle(),log.getContext(),log.getDate(),log.getLogFamily(),log.getLogStatus(),log.getId());
        return result;
    }

    @Override
    public int deleteLog(int id) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="delete from log where id=?";
        int result= qr.update(sql,id);
        return result;
    }

    @Override
    public List<Log> getDoubleConditionLog(Integer id, int pageNow, int pageSize, String logFamily, String logStatus) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where userId = ? and logFamily = ? and logStatus = ? ORDER BY id desc limit ?,? ";
        List<Log> logs1= qr.query(sql,new BeanListHandler<Log>(Log.class),id,logFamily,logStatus,pageSize*(pageNow-1),pageSize);
        return logs1;
    }

    @Override
    public List<Log> getFirstConditionLog(Integer id, int pageNow, int pageSize, String logFamily) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where userId = ? and logFamily = ? ORDER BY id desc limit ?,? ";
        List<Log> logs1= qr.query(sql,new BeanListHandler<Log>(Log.class),id,logFamily,pageSize*(pageNow-1),pageSize);
        return logs1;
    }

    @Override
    public List<Log> getSecondConditionLog(Integer id, int pageNow, int pageSize, String logStatus) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where userId = ? and logStatus = ? ORDER BY id desc limit ?,? ";
        List<Log> logs1= qr.query(sql,new BeanListHandler<Log>(Log.class),id,logStatus,pageSize*(pageNow-1),pageSize);
        return logs1;
    }



    @Override
    public List<Log> getAllLogByLogFamilyAndLogStatus(Integer id, String logFamily, String logStatus) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where userId = ? and logFamily = ? and logStatus = ?";
        List<Log> log= qr.query(sql,new BeanListHandler<Log>(Log.class),id,logFamily,logStatus);
        return log;
    }

    @Override
    public List<Log> getAllLogByLogFamily(Integer id, String logFamily) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where userId = ? and logFamily=?";
        List<Log> log= qr.query(sql,new BeanListHandler<Log>(Log.class),id,logFamily);
        return log;
    }

    @Override
    public List<Log> getAllLogByLogStatus(Integer id, String logStatus) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where userId = ? and logStatus=?";
        List<Log> log= qr.query(sql,new BeanListHandler<Log>(Log.class),id,logStatus);
        return log;
    }

    @Override
    public List<Log> checkAllLog(String logStatus,String department) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where logStatus = ? and department=?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),logStatus,department);
    }

    @Override
    public List<Log> checkLog(String logStatus,String department, int pageNow,int pageSize) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where logStatus = ? and department=? order by id desc limit ?,?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),logStatus,department,(pageNow-1)*pageSize,pageSize);
    }

    @Override
    public Log getLogById(int id) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where id=?";
        return qr.query(sql,new BeanHandler<Log>(Log.class),id);
    }

    @Override
    public int check(int id, String logStatus) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="update log set logStatus=? where id=?";
        return qr.update(sql,logStatus,id);
    }

    @Override
    public List<Log> getPassLog(String logStatus) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where logStatus=?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),logStatus);
    }

    @Override
    public List<Log> getCurrentPassLog(String logStatus,String department, int pageNow, int pageSize) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where logStatus=? and department=? order by id desc limit ?,?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),logStatus,department,(pageNow-1)*pageSize,pageSize);
    }

    @Override
    public List<Log> getFirstConditionCurrentPassLog(String logStatus,String department, String logFamily, int pageNow, int pageSize) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where logStatus=? and logFamily=? and department=? order by id desc limit ?,?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),logStatus,logFamily,department,(pageNow-1)*pageSize,pageSize);
    }

    @Override
    public List<Log> getPassLogByLogFamily(String logFamily,String department, String logStatus) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where logStatus=? and logFamily=? and department=?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),logStatus,logFamily,department);
    }

    @Override
    public List<Department> selectDepartment() throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from department";
        return qr.query(sql,new BeanListHandler<Department>(Department.class));
    }

    @Override
    public List<Log> selectAllLog() throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log order by id desc";
        return qr.query(sql,new BeanListHandler<Log>(Log.class));
    }

    @Override
    public List<Log> getCurrentAllLog(int pageNow, int page_size) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log order by id desc limit ?,?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),(pageNow-1)*page_size,page_size);
    }

    @Override
    public List<Log> selectAllLogByDoubleCondition(String department, String logFamily) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where department=? and logFamily=?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),department,logFamily);
    }

    @Override
    public List<Log> getCurrentLogByDepartmentAndLogFamily(String department, String logFamily, int pageNow, int page_size) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where department=? and logFamily=? order by id desc limit ?,?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),department,logFamily,(pageNow-1)*page_size,page_size);
    }

    @Override
    public List<Log> selectAllLogByFirstCondition(String department) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where department=?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),department);
    }

    @Override
    public List<Log> getCurrentLogByDepartment(String department, int pageNow, int page_size) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where department=? order by id desc limit ?,?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),department,(pageNow-1)*page_size,page_size);
    }

    @Override
    public List<Log> selectAllLogBySecoundCondition(String logFamily) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log  where logFamily=?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),logFamily);
    }

    @Override
    public List<Log> getCurrentLogByLogFamily(String logFamily, int pageNow, int page_size) throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from log where logFamily=? order by id desc limit ?,?";
        return qr.query(sql,new BeanListHandler<Log>(Log.class),logFamily,(pageNow-1)*page_size,page_size);
    }

    @Override
    public List<LogFamily> selectLogFamily() throws SQLException {
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from logFamily";
        return qr.query(sql,new BeanListHandler<LogFamily>(LogFamily.class));
    }


}
